package com.course.server.utils;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @description: TODO 日志
 * @author: 万物皆可喵
 * @date: 2022/9/21 14:49
 * @param:
 * @return:
 **/
@Component
@Aspect
@Slf4j
public class LogAspect {
    /**
     * define point cut.
     */
    @Pointcut("execution(* com.course.server.controller.*.*(..))")
    private void pointCutMethod() {
    }


    /**
     * 环绕通知.
     *
     * @param pjp pjp
     * @return obj
     * @throws Throwable exception
     */
    @Around("pointCutMethod()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        log.info("-----------------------");
        log.info("---Around日志通知: 进入类: {}",pjp.getTarget().getClass().getName());
        log.info("---Around通知: 进入方法: {}",methodSignature.getName());
        log.info("---Around通知: 参数列表: {}",Arrays.toString(pjp.getArgs()));
        Object o = pjp.proceed();
        log.info("---Around通知: 退出方法");
        log.info("-----------------------");
        return o;
    }

    /**
     * 前置通知.
     */
    @Before("pointCutMethod()")
    public void doBefore() {
//        System.out.println("前置通知@Before执行");
    }


    /**
     * 后置通知.
     *
     * @param result return val
     */
    @AfterReturning(pointcut = "pointCutMethod()", returning = "result")
    public void doAfterReturning(String result) {
        log.info("---后置通知:, 返回值: {}", result);
    }

    /**
     * 异常通知.
     *
     * @param e exception
     */
    @AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")
    public void doAfterThrowing(Exception e) {
        log.info("@AfterThrowing异常通知, 异常: {}" + e.getMessage());
    }

    /**
     * 最终通知.
     */
    @After("pointCutMethod()")
    public void doAfter() {
//        System.out.println("@After最终通知执行");
    }

}
